package org.jeecg.modules.xxgx.smsq.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.innerApi.common.responseDto.InvoiceRtn;
import org.jeecg.modules.xxgx.common.enums.SjlxEnum;
import org.jeecg.modules.xxgx.common.enums.TdyslxDmEnum;
import org.jeecg.modules.xxgx.common.util.InvoiceConvertUtils;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdj;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjCezs;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjExt;
import org.jeecg.modules.xxgx.djgl.entity.cezs.Cezs;
import org.jeecg.modules.xxgx.djgl.entity.tdyw.*;
import org.jeecg.modules.xxgx.djgl.enums.ZsfsEnum;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjCezsService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjExtService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjmxService;
import org.jeecg.modules.xxgx.djgl.vo.CsXxDkdjPage;
import org.jeecg.modules.xxgx.fpgl.service.IFpkjglService;
import org.jeecg.modules.xxgx.smsq.dto.CsYkSmsqDTO;
import org.jeecg.modules.xxgx.smsq.entity.CsYkSmsq;
import org.jeecg.modules.xxgx.smsq.mapper.CsYkSmsqMapper;
import org.jeecg.modules.xxgx.smsq.service.ICsYkSmsqService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 扫码申请
 * @Author: jeecg-boot
 * @Date:   2023-08-14
 * @Version: V1.0
 */
@Service
public class CsYkSmsqServiceImpl extends ServiceImpl<CsYkSmsqMapper, CsYkSmsq> implements ICsYkSmsqService {

    @Autowired
    private ICsXxDkdjService csXxDkdjService;
    @Autowired
    private ICsXxDkdjmxService dkdjmxService;
    @Autowired
    private ICsXxDkdjCezsService dkdjCezsService;
    @Autowired
    private ICsXxDkdjExtService dkdjExtService;

    @Autowired
    private IFpkjglService fpkjglService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(CsYkSmsqDTO csYkSmsqDTO) {
        TenantContext.setTenant(String.valueOf(csYkSmsqDTO.getTenantId()));
        if("1".equals(csYkSmsqDTO.getSmlx())){
            //固定码牌
            CsYkSmsq csYkSmsq = new CsYkSmsq();
            BeanUtils.copyProperties(csYkSmsqDTO,csYkSmsq);
            //保存扫码申请
            this.baseMapper.insert(csYkSmsq);

            //保存到待开表
            /*CsXxDkdj csXxDkdj = new CsXxDkdj();
            BeanUtils.copyProperties(csYkSmsqDTO,csXxDkdj);
            csXxDkdj.setId(String.valueOf(IdWorker.getId()));
            csXxDkdj.setSjlx(SjlxEnum.SM.getValue());//数据类型：扫码
            csXxDkdj.setSjly("zizhu");//数据来源：自助
            csXxDkdj.setSjyt(csYkSmsqDTO.getSsyt());
            csXxDkdj.setKhlxfs(csYkSmsqDTO.getJssjh());
            csXxDkdj.setKhlxyx(csYkSmsqDTO.getJsyx());

            csXxDkdjService.save(csXxDkdj);*/
            return Result.OK("保存成功");
        }else if("2".equals(csYkSmsqDTO.getSmlx())){
            //根据订单生成的扫码，更新待开单据表中客户信息
            if(StringUtils.isEmpty(csYkSmsqDTO.getDdid())){
                throw new RuntimeException("订单ID为空");
            }
            CsXxDkdj csXxDkdj = new CsXxDkdj();
            csXxDkdj.setTenantId(csYkSmsqDTO.getTenantId());
            csXxDkdj.setId(csYkSmsqDTO.getDdid());
            csXxDkdj.setSjlx(SjlxEnum.SM.getValue());//数据类型：扫码
            csXxDkdj.setKhmc(csYkSmsqDTO.getKhmc());
            csXxDkdj.setKhsh(csYkSmsqDTO.getKhsh());
            csXxDkdj.setKhdz(csYkSmsqDTO.getKhdz());
            csXxDkdj.setKhdh(csYkSmsqDTO.getKhdh());
            csXxDkdj.setKhyh(csYkSmsqDTO.getKhyh());
            csXxDkdj.setKhzh(csYkSmsqDTO.getKhzh());
            csXxDkdj.setZzfpbz(csYkSmsqDTO.getZzfpbz());
            csXxDkdj.setKhlxfs(csYkSmsqDTO.getJssjh());
            csXxDkdj.setKhlxyx(csYkSmsqDTO.getJsyx());
            csXxDkdj.setYjdz(csYkSmsqDTO.getYjdz());
            csXxDkdj.setYjjsr(csYkSmsqDTO.getYjjsr());
            csXxDkdjService.updateById(csXxDkdj);

            //调用开票接口
            CsXxDkdj dkdj = csXxDkdjService.getById(csYkSmsqDTO.getDdid());
            //查询待开单据明细
            List dkdjmxs = dkdjmxService.selectByMainId(csYkSmsqDTO.getDdid());

            //封装待开单据信息
            CsXxDkdjPage dkdjPage = new CsXxDkdjPage();
            BeanUtils.copyProperties(dkdj, dkdjPage);
            dkdjPage.setCsXxDkdjmxList(dkdjmxs);

            //取特定业务信息
            if (!StringUtils.isEmpty(dkdj.getTdyslxdm())) {
                List<CsXxDkdjExt> dkdjExtList = dkdjExtService.lambdaQuery().eq(CsXxDkdjExt::getFromid, csYkSmsqDTO.getDdid()).list();
                if (dkdjExtList == null && dkdjExtList.size() > 0) {
                    CsXxDkdjExt xxDkdjExt = dkdjExtList.get(0);

                    if (TdyslxDmEnum.JZFW.getValue().equals(dkdj.getTdyslxdm())) {
                        Jzfw jzfw = JSON.parseObject(xxDkdjExt.getTdywInfo(), Jzfw.class);
                        dkdjPage.setTdywJzfw(jzfw);
                    } else if (TdyslxDmEnum.HWYSFW.getValue().equals(dkdj.getTdyslxdm())) {
                        List<Hwysfwmx> hwysfwmxList = JSONArray.parseArray(xxDkdjExt.getTdywInfo(), Hwysfwmx.class);
                        dkdjPage.setTdywHwysfwList(hwysfwmxList);
                    } else if (TdyslxDmEnum.BDCXS.getValue().equals(dkdj.getTdyslxdm())) {
                        Bdcxsfw bdcxsfw = JSON.parseObject(xxDkdjExt.getTdywInfo(), Bdcxsfw.class);
                        dkdjPage.setTdywBdcxs(bdcxsfw);
                    } else if (TdyslxDmEnum.BDCJYZLFW.getValue().equals(dkdj.getTdyslxdm())) {
                        Bdcjyzlfw bdcjyzlfw = JSON.parseObject(xxDkdjExt.getTdywInfo(), Bdcjyzlfw.class);
                        dkdjPage.setTdywBdcjyzlfw(bdcjyzlfw);
                    } else if (TdyslxDmEnum.LKYSFW.getValue().equals(dkdj.getTdyslxdm())) {
                        List<Lkysfwmx> lkysfwmxList = JSONArray.parseArray(xxDkdjExt.getTdywInfo(), Lkysfwmx.class);
                        dkdjPage.setTdywLkysfwList(lkysfwmxList);
                    }
                }
            }

            //取差额征收信息
            if (!ZsfsEnum.PTZS.getValue().equals(dkdj.getZsfs())) {
                //取差额信息
                List<CsXxDkdjCezs> csXxDkdjCezsList = dkdjCezsService.lambdaQuery().eq(CsXxDkdjCezs::getFromid, csYkSmsqDTO.getDdid()).list();
                List<Cezs> cezsList = new ArrayList<>();
                csXxDkdjCezsList.forEach(csXxDkdjCezs -> {
                    Cezs cezs = new Cezs();
                    BeanUtils.copyProperties(csXxDkdjCezs, cezs);
                    cezsList.add(cezs);
                });
                dkdjPage.setCezsList(cezsList);
            }

            //将待开单据转换为待开票数据
            InvoiceParam dkfpParam = InvoiceConvertUtils.ConvertDjToFp(dkdjPage);
            Result<InvoiceRtn.InvoiceRtnData> invoiceResult =  fpkjglService.invoice(dkfpParam);

            if(invoiceResult.isSuccess()){
                return Result.OK(invoiceResult.getResult());
            } else {
                return Result.error(invoiceResult.getMessage());
            }
        }
        return null;
    }
}
